package com.ruoyi.workSafetyStandardization.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.workSafetyStandardization.mapper.MajorConfigMapper;
import com.ruoyi.workSafetyStandardization.domain.MajorConfig;
import com.ruoyi.workSafetyStandardization.service.IMajorConfigService;

/**
 * 专业配置Service业务层处理
 *
 * @author pengjie
 * @date 2023-12-14
 */
@Service
public class MajorConfigServiceImpl implements IMajorConfigService
{
    @Autowired
    private MajorConfigMapper majorConfigMapper;

    /**
     * 预览专业配置
     *
     * @param majorId 专业配置主键
     * @return 专业配置
     */
    @Override
    public MajorConfig selectMajorConfigByMajorId(String majorId)
    {
        MajorConfig majorConfig=majorConfigMapper.selectMajorConfigByMajorId(majorId);
//        List<MajorConfig> majorConfigList=majorConfigMapper.queryMajorConfigByParentId(majorConfig.getMajorId());
//        MajorConfig majorConfigVo=new MajorConfig();
//        BeanUtils.copyProperties(majorConfig,majorConfigVo);
//        majorConfigVo.setChildren(majorConfigList);
        return majorConfig;
    }

    /**
     * 查询专业关键字
     *
     * @param majorName 专业配置主键
     * @return 专业配置列表
     */
    @Override
    public List<MajorConfig> selectMajorConfigBymajorName(String majorName) {
        List<MajorConfig> list=majorConfigMapper.selectMajorConfigBymajorName(majorName);
        return  list;
//        List<MajorConfig> majorConfigVoList= list.stream().map(it->{
//            MajorConfig majorConfig=new MajorConfig();
//            BeanUtils.copyProperties(it,majorConfig);
//            majorConfig.setChildren(getChildren(it,majorConfigMapper.queryAll()));
//            return majorConfig;
//        }).collect(Collectors.toList());
//        return majorConfigVoList;
    }

    /**
     * 查询专业配置列表
     *
     * @param majorConfig 专业配置
     * @return 专业配置
     */
    @Override
    public List<MajorConfig> selectMajorConfigList(MajorConfig majorConfig)
    {
        List<MajorConfig> list=majorConfigMapper.selectMajorConfigList(majorConfig);
//        List<MajorConfig> majorConfigVoList=queryChildrenMajor(list);
        return list;
    }

    @Override
    public List<MajorConfig> selectMajorConfigListTree(MajorConfig majorConfig) {
        List<MajorConfig> list=majorConfigMapper.selectMajorConfigList(majorConfig);
        List<MajorConfig> majorConfigVoList=queryChildrenMajor(list);
        return majorConfigVoList;
    }

    /**
     * 新增专业配置
     *
     * @param majorConfig 专业配置
     * @return 结果
     */
    @Override
    public int insertMajorConfig(MajorConfig majorConfig)
    {
        majorConfig.setCreateByName(SecurityUtils.getUsername());
        majorConfig.setCreateAt(new Date());
        majorConfig.setCreateBy(SecurityUtils.getUserId().toString());
        return majorConfigMapper.insertMajorConfig(majorConfig);
    }

    /**
     * 修改专业配置
     *
     * @param majorConfig 专业配置
     * @return 结果
     */
    @Override
    public int updateMajorConfig(MajorConfig majorConfig)
    {
        return majorConfigMapper.updateMajorConfig(majorConfig);
    }

    /**
     * 批量删除专业配置
     *
     * @param majorIds 需要删除的专业配置主键
     * @return 结果
     */
    @Override
    public int deleteMajorConfigByMajorIds(String[] majorIds)
    {
        return majorConfigMapper.deleteMajorConfigByMajorIds(majorIds);
    }

    /**
     * 删除专业配置信息
     *
     * @param majorId 专业配置主键
     * @return 结果
     */
    @Override
    public int deleteMajorConfigByMajorId(String majorId)
    {
        return majorConfigMapper.deleteMajorConfigByMajorId(majorId);
    }

    /**
     * 查询专业tree
     * @param list 专业配置集合
     * @return 专业配置集合（包含子专业）
     */

    @Override
    public List<MajorConfig> queryChildrenMajor(List<MajorConfig> list) {
        List<MajorConfig> majorConfigVoList=list.stream().filter(enty-> "".equals(enty.getParentId()))
                .map(it->{
            it.setChildren(getChildren(it,list));
            return it;
                }
        ).collect(Collectors.toList());
        return majorConfigVoList;
    }

    /**
     * 查询所有专业
     * @param
     * @return
     */
    @Override
    public List<MajorConfig> queryAll(){
        return majorConfigMapper.queryAll();
    }

    /**
     * 通过父专业查子专业
     * @param
     * @return
     */

    @Override
    public List<MajorConfig> queryMajorConfigByparentId(String parentId) {
        return majorConfigMapper.queryMajorConfigByparentId(parentId);
    }

    /**查询子专业
     * @Param MajorConfig 父专业  List 所有专业列表
     * @return
     */
    List<MajorConfig> getChildren(MajorConfig root, List<MajorConfig> all){
        List<MajorConfig> list=all.stream().filter(it->it.getParentId().equals(root.getMajorId()))
                .map(major->{
            major.setChildren(getChildren(major,all));
            return major;
        }).collect(Collectors.toList());
        return list;
    }
}
